/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.swinvehicle.managedbean;

import au.edu.swin.swinvehicle.corporateclient.CorporateClientDetailedTO;
import au.edu.swin.swinvehicle.corporateclient.CorporateClientManagerException;
import au.edu.swin.swinvehicle.corporateclient.CorporateClientManagerRemote;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author Administrator
 */
public class CorporateClientJSFManagedBean {

    public static final String LIST_CORPORATECLIENTS = "listCorporateClients";
    public static final String ADD_CORPORATECLIENT = "addCorporateClient";
    public static final String EDIT_CORPORATECLIENT = "editCorporateClient";
    @EJB
    private CorporateClientManagerRemote corporateClientManagerBean;
    private CorporateClientDetailedTO corporateClient;

    /** Creates a new instance of CorporateClientJSFManagedBean */
    public CorporateClientJSFManagedBean() {
        this.corporateClient = new CorporateClientDetailedTO();
    }

    public CorporateClientDetailedTO getCorporateClient() {
        return corporateClient;
    }

    public void setCorporateClient(CorporateClientDetailedTO corporateClient) {
        this.corporateClient = corporateClient;
    }

    public List<CorporateClientDetailedTO> getCorporateClients() {
        List<CorporateClientDetailedTO> corporateClients = new ArrayList<CorporateClientDetailedTO>();
        try {
            corporateClients = this.corporateClientManagerBean.getAllDetailedCorporateClients();
        } catch (CorporateClientManagerException ex) {
            Logger.getLogger(CorporateClientJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return corporateClients;
    }

    public String addCorporateClient() {
        try {
            // Check if username is available
            if (this.corporateClientManagerBean.isUsernameAvailable(this.corporateClient.getUsername())) {
                this.corporateClientManagerBean.addCorporateClient(this.corporateClient);
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_INFO);
                msg.setSummary("Added:");
                msg.setDetail("The new corporate client has been successfully added");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralCorporateClientSuccessNotification", msg);
                return LIST_CORPORATECLIENTS;
            } else {
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                msg.setSummary("Invalid username:");
                msg.setDetail("The username is not available. Please try again.");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralCorporateClientErrorNotification", msg);
                return ADD_CORPORATECLIENT;
            }
        } catch (CorporateClientManagerException ex) {
            Logger.getLogger(CorporateClientJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot add new corporate client. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralCorporateClientErrorNotification", msg);
            return ADD_CORPORATECLIENT;
        }
    }

    public String editCorporateClient() {
        String corporateClientIdString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("corporateClientId");
        try {
            Long corporateClientId = Long.parseLong(corporateClientIdString);
            this.corporateClient = this.corporateClientManagerBean.getCorporateClient(corporateClientId);
            if (this.corporateClient == null) {
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                msg.setSummary("Invalid corporate client ID:");
                msg.setDetail("The corporate client does not exist. Please try again.");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralCorporateClientErrorNotification", msg);
                return LIST_CORPORATECLIENTS;
            } else {
                return EDIT_CORPORATECLIENT;
            }
        } catch (CorporateClientManagerException ex) {
            Logger.getLogger(CorporateClientJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot edit corporate client. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralCorporateClientErrorNotification", msg);

            return LIST_CORPORATECLIENTS;
        }
    }

    public String saveCorporateClient() {
        try {
            // Check if the new username is available
            if (this.corporateClientManagerBean.getCorporateClient(this.corporateClient.getId()).getUsername().equals(this.corporateClient.getUsername()) || this.corporateClientManagerBean.isUsernameAvailable(this.corporateClient.getUsername())) {
                this.corporateClientManagerBean.editCorporateClient(this.corporateClient);
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_INFO);
                msg.setSummary("Updated:");
                msg.setDetail("The corporate client has been successfully updated");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralCorporateClientSuccessNotification", msg);

                return LIST_CORPORATECLIENTS;
            } else {
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                msg.setSummary("Invalid username:");
                msg.setDetail("The username is not available. Please try again.");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralCorporateClientErrorNotification", msg);

                return EDIT_CORPORATECLIENT;
            }
        } catch (CorporateClientManagerException ex) {
            Logger.getLogger(CorporateClientJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            Logger.getLogger(CorporateClientJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot edit corporate client. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralCorporateClientErrorNotification", msg);
            return EDIT_CORPORATECLIENT;
        }
    }

    public String deleteCorporateClient() {
        String corporateClientIdString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("corporateClientId");
        try {
            Long corporateClientId = Long.parseLong(corporateClientIdString);
            this.corporateClientManagerBean.deleteCorporateClient(corporateClientId);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_INFO);
            msg.setSummary("Deleted:");
            msg.setDetail("The corporate client has been successfully deleted");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralCorporateClientSuccessNotification", msg);

            return LIST_CORPORATECLIENTS;
        } catch (CorporateClientManagerException ex) {
            Logger.getLogger(CorporateClientJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot delete corporate client. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralCorporateClientErrorNotification", msg);

            return LIST_CORPORATECLIENTS;
        }
    }
}
